package cn.com.assistant.controller;

import cn.com.assistant.annotation.AdminLoginToken;
import cn.com.assistant.entity.SchemaEntity;
import cn.com.assistant.entity.TableEntity;
import cn.com.assistant.mapper.SchemaMapper;
import cn.com.assistant.mapper.TableMapper;
import cn.com.assistant.service.SchemaService;
import cn.com.assistant.service.TableService;
import cn.com.assistant.utils.RequestUtil;
import cn.com.assistant.utils.ResponseMessage;
import cn.com.assistant.utils.UUIDUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/schema")
public class SchemaController {
 
    @Autowired
    SchemaService schemaService;
    @Autowired
    SchemaMapper schemaMapper;
    @Autowired
    TableMapper tableMapper;

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PostMapping("/insert")
    @ApiOperation(value = "添加属性", notes = "添加属性")
    @ApiImplicitParam(name = "schemaEntity", value = "属性", required = true, dataType = "SchemaEntity")
    public ResponseMessage insert(@RequestBody @Valid SchemaEntity schemaEntity, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        schemaEntity.setId(UUIDUtil.getUUID());
        schemaEntity.setCreatedDate(new Date());
        // 存储关联实体信息
        TableEntity tableEntity = tableMapper.selectById(schemaEntity.getPrincipalId());
        schemaEntity.setPrincipalProperty(tableEntity.getChineseName());
        boolean flag = schemaService.save(schemaEntity);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("保存成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("保存失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PutMapping("/update")
    @ApiOperation(value = "修改属性", notes = "修改属性")
    @ApiImplicitParam(name = "schemaEntity", value = "属性", required = true, dataType = "SchemaEntity")
    public ResponseMessage update(@RequestBody @Valid SchemaEntity schemaEntity, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        schemaEntity.setUpdatedDate(new Date());
        boolean flag = schemaService.updateById(schemaEntity);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("修改成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("修改失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除属性", notes = "删除属性")
    @ApiImplicitParam(name = "id", value = "属性id")
    public ResponseMessage delete(@RequestParam("id") String id) {

        boolean flag = schemaService.removeById(id);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("删除成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("删除失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping("/findById")
    @ApiOperation(value = "属性查询", notes = "属性查询")
    @ApiImplicitParam(name = "属性id", value = "属性id")
    public ResponseMessage findById(@RequestParam("id") String id) {
        SchemaEntity entity = schemaService.getById(id);
        return ResponseMessage.defaultResponse().setData(entity);
    }


    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ApiOperation(value = "属性分页列表", notes = "属性分页列表")
    @GetMapping(value = "/page")
    public ResponseMessage findByPage(@RequestParam(value = "page", defaultValue = "0") Integer pageNum,
                                      @RequestParam(value = "size", defaultValue = "10") Integer size,
                                      HttpServletRequest request) {
        Map<String, Object> filters = RequestUtil.getParameters(request);
        LambdaQueryWrapper<SchemaEntity> schemaLambdaQueryWrapper = Wrappers.lambdaQuery();
        Page<SchemaEntity> schemaPage = new Page<>(pageNum , size , true);
        IPage<SchemaEntity> schemaIPage = schemaMapper.selectPage(schemaPage , schemaLambdaQueryWrapper);
        return ResponseMessage.defaultResponse().setData(schemaIPage);
    }


}